<template>
  <div>
    <van-nav-bar
      :z-index="999"
      :fixed="true"
      left-arrow
      @click-left="onClickLeft"
      :placeholder="true"
      title="我的收藏"
    ></van-nav-bar>

    <!-- 列表 -->
    <div class="my-favorite">
      <div v-for="(item, index) in list" :key="index">
        <p @click="jumpTo(item.docid, item.lb, item.jgbh)">
          {{ item.title }}
        </p>
        <div>
          <p
            @click="jumpTo(item.docid, item.lb, item.jgbh)"
            v-if="item.lb == '1'"
          >
            政策法规
          </p>
          <p
            @click="jumpTo(item.docid, item.lb, item.jgbh)"
            v-if="item.lb == '2'"
          >
            新闻资讯
          </p>
          <p
            @click="jumpTo(item.docid, item.lb, item.jgbh)"
            v-if="item.lb == '3'"
          >
            平台公告
          </p>
          <p
            @click="jumpTo(item.docid, item.lb, item.jgbh)"
            v-if="item.lb == '4'"
          >
            交规考试秘笈
          </p>
          <p
            @click="jumpTo(item.docid, item.lb, item.schoolid)"
            v-if="item.lb == '5'"
          >
            路考秘笈
          </p>
          <p
            @click="jumpTo(item.docid, item.lb, item.jgbh)"
            v-if="item.lb == '6'"
          >
            教练
          </p>
          <p
            @click="jumpTo(item.docid, item.lb, item.jgbh)"
            v-if="item.lb == '7'"
          >
            驾校
          </p>
          <div @click="cancel(item.docid)">取消</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { MyFavorite } from "../../assets/js/api/user";
import { CancelCollection } from "../../assets/js/api/apply";
import { Toast, Dialog } from "vant";
export default {
  name: "MyFavorite",
  data() {
    return {
      list: []
    };
  },

  created() {
    let dsBridge = require("dsbridge");
    dsBridge.call("goBack", "", function() {
      history.go(-1);
    });

    MyFavorite().then(res => {
      // console.log(res);
      if (res.code == 0) {
        this.list = res.data.list;
      }
    });
  },

  methods: {
    //  取消我的收藏
    cancel(id) {
      Dialog.confirm({
        message: "确认取消"
      })
        .then(() => {
          // on confirm
          CancelCollection(id).then(res => {
            // console.log(res);
            if (res.code == 0) {
              Toast.success("取消成功");
              for (let i = 0; i < this.list.length; i++) {
                if (id == this.list[i].docid) {
                  this.list.splice(i, 1);
                }
              }
            }
          });
        })
        .catch(() => {
          // on cancel
        });
    },

    jumpTo(id, type, sid) {
      // 1 :政策法规 2 :新闻资讯 3 :平台公告 4 :考试秘笈 5 :路考秘笈 6 :教练 7 :驾校
      if (type == "1") {
        // console.log("政策法规");
        this.$router.push({ path: "/NewsDetail/" + id });
      }
      if (type == "2") {
        // console.log("新闻资讯");
        this.$router.push({ path: "/NewsDetail/" + id });
      }
      if (type == "3") {
        // console.log("平台公告");
        this.$router.push({ path: "/NewsDetail/" + id });
      }
      if (type == "4") {
        // console.log("考试秘笈");
        this.$router.push({ path: "/SecretDetails/" + id });
      }
      if (type == "5") {
        // console.log("路考秘笈");
        this.$router.push({ path: "/SecretDetails/" + id });
      }
      if (type == "6") {
        // console.log("教练");
        // console.log(id, sid);
        this.$router.push({ path: "/CoachDetail/" + id + "/" + sid });
      }
      if (type == "7") {
        // console.log("驾校");
        this.$router.push({ path: "/SchoolDetail/" + sid });
      }
    },

    onClickLeft() {
      this.$router.go(-1);
    }
  },
  components: {
    [Dialog.Component.name]: Dialog.Component
  }
};
</script>

<style></style>
